package com.heima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.reggie.common.R;
import com.heima.reggie.domain.Category;
import com.heima.reggie.domain.Setmeal;
import com.heima.reggie.domain.SetmealDish;
import com.heima.reggie.dto.SetmealDto;
import com.heima.reggie.service.CategoryService;
import com.heima.reggie.service.SetmealDishService;
import com.heima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    SetmealService setmealService;
    @Autowired
    CategoryService categoryService;
    @Autowired
    SetmealDishService setmealDishService;


    @PostMapping
    @CacheEvict(value = "setmealCache" , allEntries = true)
    public R<String> save(@RequestBody SetmealDto setmealDto){
        log.info("套餐添加===");
        setmealService.saveSetmealAndDish(setmealDto);
        return R.success("添加成功");
    }

    @GetMapping("/page")
    public R<Page> page(Integer page , Integer pageSize,String name){
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        Page<SetmealDto> setmealDtoPage = new Page<>();
        LambdaQueryWrapper<Setmeal> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.like(name != null,Setmeal::getName, name);
        dishLambdaQueryWrapper.orderByDesc(Setmeal :: getUpdateTime);
        setmealService.page(setmealPage,dishLambdaQueryWrapper);

        BeanUtils.copyProperties(setmealPage,setmealDtoPage,"records");
        List<Setmeal> records = setmealPage.getRecords();
        List<SetmealDto> stemealDtos = records.stream().map((item) -> {
            SetmealDto stemealDto = new SetmealDto();

            BeanUtils.copyProperties(item,stemealDto);

            Long categoryId = item.getCategoryId();//分类id
            //根据id查询分类对象
            Category category = categoryService.getById(categoryId);

            if(category != null){
                String categoryName = category.getName();
                stemealDto.setCategoryName(categoryName);
            }
            return stemealDto;
        }).collect(Collectors.toList());

        setmealDtoPage.setRecords(stemealDtos);
        log.info("{}",setmealDtoPage);
        return R.success(setmealDtoPage);
    }

    @GetMapping("/{id}")
    public R<SetmealDto> update01(@PathVariable Long id){
        SetmealDto setmealDto = new SetmealDto();
        List<SetmealDish> setmealDishes = setmealDishService.selectAllBySetmealId(id);
        setmealDto.setSetmealDishes(setmealDishes);

        Setmeal setmeal = setmealService.selectOneById(id);
        log.info("setmeal------------------------------{}",setmeal);
        BeanUtils.copyProperties(setmeal,setmealDto);
        log.info("setmealDto------------------------------{}",setmealDto);

        return R.success(setmealDto);

    }

    @PutMapping
    @Transactional
    @CacheEvict(value = "setmealCache" , allEntries = true)
    public R<String> update02(@RequestBody SetmealDto setmealDto){
        Long id = setmealDto.getId();
        String idStr = Long.toString(id);
        log.info("id=========={}",id);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        log.info("setmealDishes=========={}",setmealDishes);
        for(int i = 0; i < setmealDishes.size(); ++i){
            SetmealDish setmealDish = setmealDishes.get(i);
            setmealDish.setSetmealId(idStr);
        }
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDto,setmeal,"setmealDishes","categoryName");
        setmealService.updateById(setmeal);

        setmealDishService.deleteBySetmealId(id);
        setmealDishService.saveBatch(setmealDishes);
        return R.success("修改成功");
    }

    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable("status") Integer status ,  Long[] ids){
        log.info("修改套餐售卖状态——————————————————————————————修改后的status={}",status);
        setmealService.updateStatusByIdIn(status,ids);
        return R.success("状态修改成功");
    }

    @DeleteMapping
    @CacheEvict(value = "setmealCache" , allEntries = true)
    public R<String> delete(Long[] ids){
        setmealService.deleteSetmealAndDish(ids);
        return R.success("删除成功");
    }

    @GetMapping("/list")
    @Cacheable(value = "setmealCache" , key = "'setmeal' + #setmeal.categoryId")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(queryWrapper);

        return R.success(list);
    }
}
